package com.headcaselabs.lb.client;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;

import com.headcaselabs.lb.common.Record;

/* 
Copyright 2007, HeadCase Humanufacturing, Inc.

	Licensed under the Apache License, Version 2.0 (the "License"); you
	may not use this file except in compliance with the License. You may
	obtain a copy of the License at
	
	   http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
	implied. See the License for the specific language governing
	permissions and limitations under the License.
*/
/**
 * Parses a XML file for zone information.
 * @author Florian Leibert
 *
 */
public class ZoneFileImporter {
	
	public final static String AUTH_STR= "NS";
	public final static String SOA_STR= "SOA";
	
	public void log(String log, int level) {
		
	}
	@SuppressWarnings("unchecked")
	public Hashtable<Query,Record> importStaticZones(String path) {
		Document doc = XmlDocumentFactory.parseXML(new File(path));
		Element root = doc.getRootElement();
		List<Element> zones = root.getChildren("zone");
		List<Element> records = new ArrayList<Element>();
		Hashtable<Query, Record>			entries;
		Hashtable<Query, Record>			allEntries =  new Hashtable<Query, Record>();
		Query q;
		Record r;
		String zone;
		for (Element z : zones) {
			entries = new Hashtable<Query, Record>();
			records = z.getChildren("record");
			zone = z.getAttributeValue("name");
			for (Element e : records) {
				q = new Query(e.getAttributeValue("type"),e.getAttributeValue("name"));
				if (entries.get(q)==null) {
					r = new Record(e.getAttributeValue("name"),e.getAttributeValue("type"));
					r.setId(e.getAttributeValue("id"));
					r.setLoadbalanced(false);
					r.setNetClass(e.getAttributeValue("class"));
					r.setTtl(Integer.parseInt(e.getAttributeValue("ttl")));
					r.getContent().add(e.getValue());
					r.setZone(zone);
					entries.put(q, r);
				} else {
					r = entries.get(q);
					r.getContent().add(e.getValue());
				}
				if (PowerPipe.LOG) {
					log(q.toString(),PowerPipe.LOG_INFO);
					log(r.toString(),PowerPipe.LOG_INFO);
				}
			}
			//Construct authority for the zone:
			
			allEntries.putAll(entries);
		}
		zones = root.getChildren("lbzone");
		for (Element e : zones) {
			q = new Query(e.getAttributeValue("type"),e.getAttributeValue("name"));
			if (allEntries.get(q)!=null) {
				throw new RuntimeException("Configuration file faulty. You cannot mix lbzones and static zones.");
			}
			r = new Record(e.getAttributeValue("name"),e.getAttributeValue("type"));
			r.setLoadbalanced(true);
			r.setNetClass(e.getAttributeValue("class"));
			r.setTtl(Integer.parseInt(e.getAttributeValue("ttl")));
			
			allEntries.put(q,r);
			if (PowerPipe.LOG) {
				log(q.toString(),PowerPipe.LOG_INFO);
				log(r.toString(),PowerPipe.LOG_INFO);
			}
		}
		return allEntries;
	}
	
	
	
	
}
